<%--
  Created by IntelliJ IDEA.
  User: yoka24443
  Date: 2018-4-8
  Time: 20:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="java.sql.*" contentType="text/html;charset=UTF-8" language="java" %>
<%
    Connection conn = null;
    Statement statement = null;
    String studentNO = request.getParameter("studentNO");
    StringBuilder output = new StringBuilder();
    int effectCount = 0;

    try {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.驱动管理器获取数据库链接
        conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/school?user=root&password=&useUnicode=true&characterEncoding=utf8");
        //3.获取statement，执行sql
        statement = conn.createStatement();

        StringBuilder sql = new StringBuilder();
        String transferStr;
        if(studentNO != null && studentNO.trim().length() > 0) {
            sql.append(String.format("update student set studentNO = '%s'", studentNO));
            // 姓名
            if(request.getParameter("realName") != null) {
                transferStr = new String(request.getParameter("realName").getBytes("ISO-8859-1"), "utf-8");
                sql.append(String.format(", realName = '%s'", transferStr));
                output.append("姓名：").append(transferStr).append("<br/>");
            }
            // 班级名称
            if(request.getParameter("className") != null) {
                transferStr = new String(request.getParameter("className").getBytes("ISO-8859-1"), "utf-8");
                sql.append(String.format(", className = '%s'", transferStr));
                output.append("班级名称：").append(transferStr).append("<br/>");
            }
            // 年龄
            if(request.getParameter("age") != null) {
                sql.append(String.format(", age = %d", Integer.parseInt(request.getParameter("age"))));
                output.append("年龄：").append(request.getParameter("age")).append("<br/>");
            }
            // 电话
            if(request.getParameter("tele") != null) {
                sql.append(String.format(", tele = '%s'", request.getParameter("tele")));
                output.append("电话：").append(request.getParameter("tele")).append("<br/>");
            }
            // 地址
            if(request.getParameter("address") != null) {
                transferStr = new String(request.getParameter("address").getBytes("ISO-8859-1"), "utf-8");
                sql.append(String.format(", address = '%s'", transferStr));
                output.append("地址：").append(transferStr).append("<br/>");
            }
            // 微信
            if(request.getParameter("wechat") != null) {
                transferStr = new String(request.getParameter("wechat").getBytes("ISO-8859-1"), "utf-8");
                sql.append(String.format(", wechat = '%s'", transferStr));
                output.append("微信：").append(transferStr).append("<br/>");
            }
            // QQ
            if(request.getParameter("qq") != null) {
                sql.append(String.format(", qq = '%s'", request.getParameter("qq")));
                output.append("QQ：").append(request.getParameter("qq")).append("<br/>");
            }
            // 查询条件：学号
            sql.append(String.format(" where studentNO = '%s'", studentNO));
        }

        if (sql.length() > 0) {
            //4.执行sql返回结果集并解析
//            out.println(sql.toString());
            effectCount = statement.executeUpdate(sql.toString());
        }

    } catch (ClassNotFoundException e) {
        out.println("连接数据库失败！");
        e.printStackTrace();
    } catch (SQLException e) {
        out.println("SQL执行出错！");
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (statement != null) {
                statement.close();
            }
            if(conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
%>
<html>
<head>
    <title>modify student</title>
</head>
<body>
    <a href="StudentList.jsp">返回</a>
    <p>
        <h3>学生&nbsp;<font color="blue"><%=studentNO%></font>&nbsp;的信息更新成功后，如下：</h3>
        <%=output.toString()%>
    </p>
</body>
</html>
